package spring.library.LibraryProject.controller;

import spring.library.LibraryProject.constant.constants;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import spring.library.LibraryProject.model.bookInfo;
import spring.library.LibraryProject.model.pageResponse;
import spring.library.LibraryProject.model.response;
import spring.library.LibraryProject.server.booksServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 一个图书管理系统
 * 包括登录界面，图书展示界面，增加图书界面，删除图书界面
 * 成功实现
 * server已经可以直接调用mapper dao了，没必要又实用dao来调mapper
 * 实现翻页功能
 * 现在只需要加上日志和使用枚举来表示图书状态
 * Controller要对数据进行校验，再将参数传给server
 * 强制登录
 * 当每次成功登录之后会创建session，并放回sessionID，之后每次的访问都需要判断sessionID
 */
@Slf4j
@CrossOrigin("http://127.0.0.1:5500")
@RestController
@RequestMapping("library")
public class booksController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private booksServer booksServer;

    /**
     * 登录界面，根据前端传来的账号密码来判断账户是否存在
     * 参数               类型             是否必须
     * username          String              是
     * password          String              是
     */

    @RequestMapping("login")
    public response login(String username, String password, HttpSession session) {
        logger.info("[{}] 登录信息：username: " + username + " password " + password, "login");
        if (booksServer.verify(username, password)) {
            bookInfo b = new bookInfo();
            b.setAuthor(username);
            session.setAttribute(constants.SESSION_USER_KEY, b);
            return new response().success(true);
        } else {
            return new response().fail("账号或密码错误");
        }
    }

    /**
     * 添加图书
     * 将前端发来的书本数据储存下来
     * 参数               类型
     * name             String
     * author           String
     * count            String
     * price            String
     * publisher        String
     * state            String
     */
    @RequestMapping("/addBook")
    public response addBook(String bookName, String author, String count, String price
            , String publish, String status) {
        logger.info("[{}]添加图书: 信息bookName: " + bookName + " author: " + author + " count: " + count
                + " price: " + price + " publish: " + publish + " status: " + status, "addBook");
        if (bookName.isEmpty() || author.isEmpty() || count.isEmpty() || price.isEmpty() || publish.isEmpty() || status.isEmpty()) {
            logger.error("[{}]添加图书: 参数缺失，有必须的参数为传递", "addBook");
            return new response().fail("参数错误");
        }

        bookInfo b = new bookInfo();
        b.setName(author);
        b.setAuthor(author);
        b.setCount(Integer.parseInt(count));
        b.setPrice(Integer.parseInt(price));
        b.setPublisher(publish);
        b.setState(Integer.parseInt(status));
        return new response().success(booksServer.addBook(b));
    }

    /**
     * 删除book
     * 传入一个参数bookId表示要删除的书
     * 参数           类型
     * bookId         String
     */
    @RequestMapping("delBook")
    public response delBook(String id) {
        return new response().success(booksServer.del(Integer.parseInt(id)));
    }

    /**
     * 批量删除
     * 传入一个id数组，数组中包括的是这些要删的书的id
     * 参数              类型
     * delIds           Integer[]
     */
    @RequestMapping("dels")
    public response dels(@RequestBody Map<String, List<String>> bookId) {

        String[] bookIds = bookId.get("bookId").toArray(new String[0]);

        logger.info("[{}]删除图书 ids: " + bookIds.toString(), "dels");

        return new response().success(booksServer.dels(bookIds));
    }

    /**
     * 修改book
     * 先给一个id来找到指定的书，在将书的内容修改为指定的内容
     * 参数               类型
     * id               String
     * name             String
     * author           String
     * count            String
     * price            String
     * publisher        String
     * state            String
     */
    @RequestMapping("adjust")
    public response adjust(String bookId, String bookName, String author, String count, String price
            , String publish, String status) {
        logger.info("[{}] 调整图书: bookId: " + bookId + " bookName: " + bookName +
                " author: " + author + " count: " + count + "status: " + status, "adjust");

        return new response().success(booksServer.adjust(Integer.parseInt(bookId), bookName, author, count, price, publish, status));
    }

    /**
     * 分页
     * 返回每页对应的数据信息 可以根据数据的个数来确定有多少页
     */
    @RequestMapping("getPage")
    public response getPage(String currentPage) {
        pageResponse pageRequest = new pageResponse();
        pageRequest.setCurrentPage(Integer.parseInt(currentPage));
        return new response().success(true, booksServer.getPageInfo(pageRequest));

    }

}


